﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace FreeBroker
{
    public partial class BrokerMainForm : Form
    {
        private void groupBoxChart_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
        }

        private void groupBoxChart_MouseEnter(object sender, EventArgs e)
        {
            this.Cursor = Cursors.SizeAll;
        }

        private void groupBoxChart_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (sender.GetType() == typeof(GroupBox))
                {
                    GroupBox box = (GroupBox)sender;
                    box.DoDragDrop(box.Name, DragDropEffects.Move);
                }
                else if (sender.GetType() == typeof(Label))
                {
                    Label lbl = (Label)sender;
                    GroupBox box = (GroupBox)lbl.Parent;
                    box.DoDragDrop(box.Name, DragDropEffects.Move);
                }
            }
        }

        private Size GetCurrentThumbSize()
        {
            if (trackBarFrameSize.InvokeRequired)
            {
                object retValue = trackBarFrameSize.Invoke(new GetCurrentThumbSizeDelegate(GetCurrentThumbSize));
                return (Size)retValue;
            }
            int myValue = 40 + (10 * trackBarFrameSize.Value);
            float width = 1311;
            float height = 992;
            float multiplier = height / width;
            return new Size(myValue, (int)(myValue * multiplier));
        }
        private delegate Size GetCurrentThumbSizeDelegate();

        private void ChangeThumbSizes()
        {
            this.chartThumbSize = GetCurrentThumbSize();

            foreach (GroupBox box in flowLayoutPanel.Controls)
            {
                foreach (Control control in box.Controls)
                {
                    if (control.GetType() == typeof(Button))
                    {
                        box.Size = new Size(chartThumbSize.Width + 20, chartThumbSize.Height + 50);
                        break;
                    }
                }
            }
        }

        private void groupBoxChartOnDragEnter(object obj, DragEventArgs dea)
        {
            string[] formats = dea.Data.GetFormats();

            if (dea.Data.GetDataPresent(DataFormats.Text))
            {
                dea.Effect = DragDropEffects.Move;
            }
            else
            {
                dea.Effect = DragDropEffects.None;
            }
        }

        private void groupBoxChartOnDragDrop(object obj, DragEventArgs dea)
        {
            if (dea.Effect == DragDropEffects.Move)
            {
                Point point = new Point(dea.X, dea.Y);
                Point pClient = flowLayoutPanel.PointToClient(point);
                Point pScreens = flowLayoutPanel.PointToScreen(point);
                Control cntl = flowLayoutPanel.Controls[0];
                Point cntlPoint = cntl.Bounds.Location;
                Size cntlSize = cntl.Bounds.Size;

                Control child = flowLayoutPanel.GetChildAtPoint(pClient);
                if (child != null)
                {
                    int newIndex = flowLayoutPanel.Controls.GetChildIndex(child);
                    string data = (string)dea.Data.GetData(DataFormats.Text);
                    Control[] contl = flowLayoutPanel.Controls.Find(data, false);
                    if (contl.Length > 0)
                    {
                        int oldIndex = flowLayoutPanel.Controls.GetChildIndex(contl[0]);
                        this.MoveProcess(oldIndex, newIndex);
                        BrokerObject brokerObj = this.processes[(int)contl[0].Tag];
                        Console.WriteLine("Moving process <" + brokerObj.Name + "> from <" + oldIndex + "> to <" + newIndex + ">...");
                        flowLayoutPanel.Controls.SetChildIndex(contl[0], newIndex);
                    }
                }
            }
        }
    }
    
}
